//Filename    : CalendarItemDlg.cs
//Part of     : PCSAPI C# examples
//Description : Implementation of "New Calendar Item" dialog
//Version     : 3.2
//
//This example is only to be used with PC Connectivity API version 2.2.
//Compability ("as is") with future versions is not quaranteed.
//
//Copyright (c) 2007 Nokia Corporation.
//
//This material, including but not limited to documentation and any related
//computer programs, is protected by intellectual property rights of Nokia
//Corporation and/or its licensors.
//All rights are reserved. Reproducing, modifying, translating, or
//distributing any or all of this material requires the prior written consent
//of Nokia Corporation. Nokia Corporation retains the right to make changes
//to this material at any time without notice. A copyright license is hereby
//granted to download and print a copy of this material for personal use only.
//No other license to any other intellectual property rights is granted. The
//material is provided "as is" without warranty of any kind, either express or
//implied, including without limitation, any warranty of non-infringement,
//merchantability and fitness for a particular purpose. In no event shall
//Nokia Corporation be liable for any direct, indirect, special, incidental,
//or consequential loss or damages, including but not limited to, lost profits
//or revenue,loss of use, cost of substitute program, or loss of data or
//equipment arising out of the use or inability to use the material, even if
//Nokia Corporation has been advised of the likelihood of such damages occurring.

using System.Windows.Forms;
namespace CSPimNavigator.NET
{
    public partial class CalendarItemDlg
    {
        public CalendarItemDlg()
        {
            InitializeComponent();
        }
        //
        // ClearTime, hour,minute and second values from date are set to zero  
        //
        private System.DateTime ClearTime(System.DateTime Datum)
        {
            System.DateTime tmpDate = Datum;
            tmpDate = new System.DateTime(tmpDate.Year, tmpDate.Month, tmpDate.Day, 0, 0, 0);
            return tmpDate;
        }

        private void OK_Button_Click(object sender, System.EventArgs e)
        {
            this.DialogResult = System.Windows.Forms.DialogResult.OK;
            this.Close();
        }

        private void Cancel_Button_Click(object sender, System.EventArgs e)
        {
            this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            this.Close();
        }

        // Sync date/time values after change
        private void DTPickerNoteBeginDate_ValueChanged(object sender, System.EventArgs e)
        {
            if (bEndTimeIsBeginTime)
            {
                this.DTPickerNoteEndDate.Value = this.DTPickerNoteBeginDate.Value;
                this.DTPickerNoteEndTime.Value = this.DTPickerNoteBeginDate.Value;
            }
            if (!this.DTPickerAlarmDate.Enabled)
            {
                this.DTPickerAlarmDate.Value = this.DTPickerNoteBeginDate.Value.AddMinutes( -15.0);
                this.DTPickerAlarmTime.Value = this.DTPickerNoteBeginDate.Value.AddMinutes( -15.0);
            }
        }

        // Sync date/time values after change
        private void DTPickerNoteBeginTime_ValueChanged(object sender, System.EventArgs e)
        {
            System.DateTime tmp1 = this.DTPickerNoteBeginDate.Value;
            System.DateTime tmp2 = this.DTPickerNoteBeginTime.Value;
            this.DTPickerNoteBeginDate.Value = new System.DateTime(tmp1.Year, tmp1.Month, tmp1.Day, tmp2.Hour, tmp2.Minute, tmp2.Second);
            if (bEndTimeIsBeginTime)
            {
                this.DTPickerNoteEndDate.Value = this.DTPickerNoteBeginDate.Value;
                this.DTPickerNoteEndTime.Value = this.DTPickerNoteBeginDate.Value;
            }
        }

        // Sync date/time values after change
        private void DTPickerNoteEndTime_ValueChanged(object sender, System.EventArgs e)
        {
            System.DateTime tmp1 = this.DTPickerNoteEndDate.Value;
            System.DateTime tmp2 = this.DTPickerNoteEndTime.Value;
            this.DTPickerNoteEndDate.Value = new System.DateTime(tmp1.Year, tmp1.Month, tmp1.Day, tmp2.Hour, tmp2.Minute, tmp2.Second);
        }

        // Sync date/time values after change
        private void DTPickerAlarmTime_ValueChanged(object sender, System.EventArgs e)
        {
            System.DateTime tmp1 = this.DTPickerAlarmDate.Value;
            System.DateTime tmp2 = this.DTPickerAlarmTime.Value;
            this.DTPickerAlarmDate.Value = new System.DateTime(tmp1.Year, tmp1.Month, tmp1.Day, tmp2.Hour, tmp2.Minute, tmp2.Second);
        }

        private void CalendarItemDlg_Load(object sender, System.EventArgs e)
        {
            // On load set "Meeting" type as default
            ComboBoxType.SelectedIndex = 0;
            ComboAlarm.SelectedIndex = 0;
            ComboRecurrence.SelectedIndex = 0;

            // Set & hide Todo options
            ComboTodoPrior.SelectedIndex = 0;
            ComboTodoAction.SelectedIndex = 0;
            ComboTodoPrior.Visible = false;
            ComboTodoAction.Visible = false;

            // Begin and End Date/Time individually selecteable
            bEndTimeIsBeginTime = false;
            DTPickerNoteBeginDate.Enabled = true;
            DTPickerNoteBeginTime.Enabled = true;
            DTPickerNoteEndDate.Enabled = true;
            DTPickerNoteEndTime.Enabled = true;

            // Alarm is enabled
            ComboAlarm.Enabled = true;
            DTPickerAlarmDate.Enabled = false;
            DTPickerAlarmTime.Enabled = false;

            // Recurrence enabled
            ComboRecurrence.Enabled = true;
            CheckBoxRecEnd.Enabled = false;
            DTPickerRecDate.Enabled = false;
            DTPickerRecTime.Enabled = false;

            // Description & Location info 
            LabelSubject.Text = "Description:";
            TextBoxNote.Enabled = true;
            TextBoxNote.Text = "";
            TextBoxNote.Multiline = false;
            LabelLoc.Text = "Location:";
            TextBoxLocation.Enabled = true;
            TextBoxLocation.Multiline = false;
            TextBoxLocation.Text = "";

        }

        private void ComboBoxType_SelectedIndexChanged(object sender, System.EventArgs e)
        {
            int iItem = ComboBoxType.SelectedIndex;
            // Set fields according item type
            switch (iItem)
            {
                case 0: // Meeting
                    // Set & hide Todo options
                    ComboTodoPrior.SelectedIndex = 0;
                    ComboTodoAction.SelectedIndex = 0;
                    ComboTodoPrior.Visible = false;
                    ComboTodoAction.Visible = false;

                    // Begin and End Date/Time individually selecteable
                    bEndTimeIsBeginTime = false;
                    DTPickerNoteBeginDate.Enabled = true;
                    DTPickerNoteBeginTime.Enabled = true;
                    DTPickerNoteEndDate.Enabled = true;
                    DTPickerNoteEndTime.Enabled = true;

                    // Alarm is enabled
                    ComboAlarm.SelectedIndex = 0;
                    ComboAlarm.Enabled = true;
                    DTPickerAlarmDate.Enabled = false;
                    DTPickerAlarmTime.Enabled = false;

                    // Recurrence enabled
                    ComboRecurrence.SelectedIndex = 0;
                    ComboRecurrence.Enabled = true;
                    CheckBoxRecEnd.Enabled = false;
                    DTPickerRecDate.Enabled = false;
                    DTPickerRecTime.Enabled = false;

                    // Description & Location info 
                    LabelSubject.Text = "Description:";
                    TextBoxNote.Enabled = true;
                    TextBoxNote.Multiline = false;
                    TextBoxNote.Text = "";
                    LabelLoc.Text = "Location:";
                    TextBoxLocation.Enabled = true;
                    TextBoxLocation.Multiline = false;
                    TextBoxLocation.Text = "";
                    break;
                case 1: // Call
                    // Set & hide Todo options
                    ComboTodoPrior.SelectedIndex = 0;
                    ComboTodoAction.SelectedIndex = 0;
                    ComboTodoPrior.Visible = false;
                    ComboTodoAction.Visible = false;

                    // Begin and End connected, Date/Time individually selecteable
                    bEndTimeIsBeginTime = true;
                    DTPickerNoteBeginDate.Enabled = true;
                    DTPickerNoteBeginTime.Enabled = true;
                    DTPickerNoteEndDate.Enabled = false;
                    DTPickerNoteEndTime.Enabled = false;

                    // Alarm is enabled
                    ComboAlarm.SelectedIndex = 0;
                    ComboAlarm.Enabled = true;
                    DTPickerAlarmDate.Enabled = false;
                    DTPickerAlarmTime.Enabled = false;

                    // Recurrence enabled
                    ComboRecurrence.SelectedIndex = 0;
                    ComboRecurrence.Enabled = true;
                    CheckBoxRecEnd.Enabled = false;
                    DTPickerRecDate.Enabled = false;
                    DTPickerRecTime.Enabled = false;

                    // Name & Number info 
                    LabelSubject.Text = "Name:";
                    TextBoxNote.Enabled = true;
                    TextBoxNote.Multiline = false;
                    TextBoxNote.Text = "";
                    LabelLoc.Text = "Number:";
                    TextBoxLocation.Enabled = true;
                    TextBoxLocation.Multiline = false;
                    TextBoxLocation.Text = "";
                    break;
                case 2: // Birthday
                    // Set & hide Todo options
                    ComboTodoPrior.SelectedIndex = 0;
                    ComboTodoAction.SelectedIndex = 0;
                    ComboTodoPrior.Visible = false;
                    ComboTodoAction.Visible = false;

                    // Begin and End connected, only Date selectable
                    bEndTimeIsBeginTime = true;
                    DTPickerNoteBeginDate.Enabled = true;
                    DTPickerNoteBeginTime.Enabled = false;
                    DTPickerNoteEndDate.Enabled = false;
                    DTPickerNoteEndTime.Enabled = false;
                    DTPickerNoteBeginTime.Value = ClearTime(DTPickerNoteBeginTime.Value);
                    DTPickerNoteEndTime.Value = ClearTime(DTPickerNoteEndTime.Value);

                    // Alarm is enabled
                    ComboAlarm.SelectedIndex = 0;
                    ComboAlarm.Enabled = true;
                    DTPickerAlarmDate.Enabled = false;
                    DTPickerAlarmTime.Enabled = false;

                    // Recurrence enabled, yearly preselected
                    ComboRecurrence.SelectedIndex = 4;
                    ComboRecurrence.Enabled = true;
                    CheckBoxRecEnd.Enabled = true;
                    DTPickerRecDate.Enabled = false;
                    DTPickerRecTime.Enabled = false;

                    // Name & Year info 
                    LabelSubject.Text = "Name:";
                    TextBoxNote.Enabled = true;
                    TextBoxNote.Multiline = false;
                    TextBoxNote.Text = "";
                    LabelLoc.Text = "Year:";
                    TextBoxLocation.Enabled = true;
                    TextBoxLocation.Multiline = false;
                    TextBoxLocation.Text = "";
                    break;
                case 3: // Memo
                    // Set & hide Todo options
                    ComboTodoPrior.SelectedIndex = 0;
                    ComboTodoAction.SelectedIndex = 0;
                    ComboTodoPrior.Visible = false;
                    ComboTodoAction.Visible = false;
                    //
                    bEndTimeIsBeginTime = false;

                    // Begin and End date selectable, but only Date
                    DTPickerNoteBeginDate.Enabled = true;
                    DTPickerNoteBeginTime.Enabled = false;
                    DTPickerNoteEndDate.Enabled = true;
                    DTPickerNoteEndTime.Enabled = false;
                    DTPickerNoteBeginTime.Value =ClearTime(DTPickerNoteBeginTime.Value);
                    DTPickerNoteEndTime.Value = ClearTime(DTPickerNoteEndTime.Value);

                    // Alarm is enabled
                    ComboAlarm.SelectedIndex = 0;
                    ComboAlarm.Enabled = true;
                    DTPickerAlarmDate.Enabled = false;
                    DTPickerAlarmTime.Enabled = false;

                    // Recurrence enabled
                    ComboRecurrence.SelectedIndex = 0;
                    ComboRecurrence.Enabled = true;
                    CheckBoxRecEnd.Enabled = false;
                    DTPickerRecDate.Enabled = false;
                    DTPickerRecTime.Enabled = false;

                    // Only memo field
                    LabelSubject.Text = "Memo:";
                    TextBoxNote.Enabled = true;
                    TextBoxNote.Multiline = true;
                    TextBoxNote.Text = "";
                    LabelLoc.Text = "";
                    TextBoxLocation.Enabled = false;
                    TextBoxLocation.Multiline = false;
                    TextBoxLocation.Text = "";
                    break;
                case 4: // Reminder
                    // Set & hide Todo options
                    ComboTodoPrior.SelectedIndex = 0;
                    ComboTodoAction.SelectedIndex = 0;
                    ComboTodoPrior.Visible = false;
                    ComboTodoAction.Visible = false;

                    // Begin and End connected, only date selectable
                    bEndTimeIsBeginTime = true;
                    DTPickerNoteBeginDate.Enabled = true;
                    DTPickerNoteBeginTime.Enabled = false;
                    DTPickerNoteEndDate.Enabled = false;
                    DTPickerNoteEndTime.Enabled = false;
                    DTPickerNoteBeginTime.Value = ClearTime(DTPickerNoteBeginTime.Value);
                    DTPickerNoteEndTime.Value = ClearTime(DTPickerNoteEndTime.Value);

                    // Alarm is enabled
                    ComboAlarm.SelectedIndex = 0;
                    ComboAlarm.Enabled = true;
                    DTPickerAlarmDate.Enabled = false;
                    DTPickerAlarmTime.Enabled = false;

                    // Recurrence enabled
                    ComboRecurrence.SelectedIndex = 0;
                    ComboRecurrence.Enabled = true;
                    CheckBoxRecEnd.Enabled = false;
                    DTPickerRecDate.Enabled = false;
                    DTPickerRecTime.Enabled = false;

                    // Only about field
                    LabelSubject.Text = "Remind about:";
                    TextBoxNote.Enabled = true;
                    TextBoxNote.Multiline = true;
                    TextBoxNote.Text = "";
                    LabelLoc.Text = "";
                    TextBoxLocation.Enabled = false;
                    TextBoxLocation.Multiline = false;
                    TextBoxLocation.Text = "";
                    break;

                case 5: // Note
                    // Set & hide Todo options
                    ComboTodoPrior.SelectedIndex = 0;
                    ComboTodoAction.SelectedIndex = 0;
                    ComboTodoPrior.Visible = false;
                    ComboTodoAction.Visible = false;

                    // Begin and End connected, only date selectable
                    bEndTimeIsBeginTime = true;
                    DTPickerNoteBeginDate.Enabled = true;
                    DTPickerNoteBeginTime.Enabled = false;
                    DTPickerNoteEndDate.Enabled = false;
                    DTPickerNoteEndTime.Enabled = false;
                    DTPickerNoteBeginTime.Value = ClearTime(DTPickerNoteBeginTime.Value);
                    DTPickerNoteEndTime.Value = ClearTime(DTPickerNoteEndTime.Value);

                    // No alarm
                    ComboAlarm.SelectedIndex = 0;
                    ComboAlarm.Enabled = false;
                    DTPickerAlarmDate.Enabled = false;
                    DTPickerAlarmTime.Enabled = false;
                    ComboRecurrence.Enabled = false;

                    // No recurrence
                    ComboRecurrence.SelectedIndex = 0;
                    CheckBoxRecEnd.Enabled = false;
                    DTPickerRecDate.Enabled = false;
                    DTPickerRecTime.Enabled = false;

                    // Only Note field
                    LabelSubject.Text = "Note:";
                    TextBoxNote.Enabled = true;
                    TextBoxNote.Multiline = true;
                    TextBoxNote.Text = "";
                    LabelLoc.Text = "";
                    TextBoxLocation.Enabled = false;
                    TextBoxLocation.Multiline = false;
                    TextBoxLocation.Text = "";
                    break;

                case 6: // Todo
                    // Set & show Todo options
                    ComboTodoPrior.SelectedIndex = 0;
                    ComboTodoAction.SelectedIndex = 0;
                    ComboTodoPrior.Visible = true;
                    ComboTodoAction.Visible = true;

                    // Begin and End connected, date and time selectable
                    bEndTimeIsBeginTime = true;
                    DTPickerNoteBeginDate.Enabled = true;
                    DTPickerNoteBeginTime.Enabled = true;
                    DTPickerNoteEndDate.Enabled = false;
                    DTPickerNoteEndTime.Enabled = false;

                    // Alarm is enabled
                    ComboAlarm.SelectedIndex = 0;
                    ComboAlarm.Enabled = true;
                    DTPickerAlarmDate.Enabled = false;
                    DTPickerAlarmTime.Enabled = false;

                    // No recurrence
                    ComboRecurrence.SelectedIndex = 0;
                    ComboRecurrence.Enabled = false;
                    CheckBoxRecEnd.Enabled = false;
                    DTPickerRecDate.Enabled = false;
                    DTPickerRecTime.Enabled = false;

                    // Only Subject field
                    LabelSubject.Text = "Subject:";
                    TextBoxNote.Enabled = true;
                    TextBoxNote.Multiline = true;
                    TextBoxNote.Text = "";
                    LabelLoc.Text = "";
                    TextBoxLocation.Enabled = false;
                    TextBoxLocation.Multiline = false;
                    TextBoxLocation.Text = "";
                    break;
                default:
                    break; // Do nothing
            }
        }

        private void ComboAlarm_SelectedIndexChanged(object sender, System.EventArgs e)
        {
            int iItem = ComboAlarm.SelectedIndex;
            // If alarm is set enable alarm time fields
            if (iItem >= 1)
            {
                DTPickerAlarmDate.Enabled = true;
                DTPickerAlarmTime.Enabled = true;
            }
            else
            {
                DTPickerAlarmDate.Enabled = false;
                DTPickerAlarmTime.Enabled = false;
            }
        }

        private void ComboRecurrence_SelectedIndexChanged(object sender, System.EventArgs e)
        {
            int iItem = ComboRecurrence.SelectedIndex;
            // If recurrence is set enable end date selection
            if (iItem >= 1)
            {
                CheckBoxRecEnd.Enabled = true;
            }
            else
            {
                CheckBoxRecEnd.Enabled = false;
            }
        }

        private void CheckBoxRecEnd_CheckedChanged(object sender, System.EventArgs e)
        {
            // If recurrence end time is set, enable date/time fiels
            if (CheckBoxRecEnd.Checked)
            {
                DTPickerRecDate.Enabled = true;
                DTPickerRecTime.Enabled = true;
            }
            else
            {
                DTPickerRecDate.Enabled = false;
                DTPickerRecTime.Enabled = false;
            }
        }

        // Set if begin and end times are connected
        private bool bEndTimeIsBeginTime = false;

    }
}
